// 引入加密库
const CryptoJS = require('@/utils/crypto-js/index.js');
import { globalConfig } from '@/common/config.js'
const PRIVATE_KEY = 'c81b7d38b57d7dbabcd3c38dc37446f4';
const SIGN_KEY = 'tbq_app';
// 加密对象
export function encrypt(word) {
	var key = CryptoJS.enc.Utf8.parse(PRIVATE_KEY) 
	var srcs = CryptoJS.enc.Utf8.parse(word)
	var encrypted = CryptoJS.AES.encrypt(srcs, key, {
		mode: CryptoJS.mode.ECB,
		padding: CryptoJS.pad.Pkcs7,
	});
	return encrypted.toString();
}

// 格式化拦截器中的请求参数
export function formatRequestParam(param) {
		// console.log('param=====================1');
		// console.log(param);
	let tempParam = {};
	let resParamArr = [];
	// 去除原型链中的属性
	for (var key in param) {
		if (param.hasOwnProperty(key) && key != 'sign' && param[key] != '' && param[key] != undefined && param[key] != null) {
			tempParam[key] = param[key];
		}
	}
	let tempKeys = Object.keys(tempParam);
	tempKeys.sort();
	for (let k of tempKeys) {
		resParamArr.push(k + '=' + tempParam[k])
	}
	var sign = resParamArr.join('&');
	if (sign !== '') {
		sign = sign + '&'
	}
	sign += SIGN_KEY;
	sign = CryptoJS.MD5(sign).toString();
	// console.log(tempParam);
		// console.log('param=====================2');
	// 最终发送的字段
	return {
		sign:sign,
		timestamp: +Date.now(),
		data: encrypt(JSON.stringify(tempParam)),
	}
}
